﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using AutoPartsSearch.Domain.Abstract.Repositories;
using AutoPartsSearch.Domain.Abstract;
using AutoPartsSearch.Domain.Entities;
using System.Security.Principal;
using AutoPartsSearch.Domain.Entities.Enums;
using AutoPartsSearch.Domain.Exceptions;
using AutoPartsSearch.Web.ViewModels;
using AutoPartsSearch.Web.ViewModels.SellerSearchQuery;

namespace AutoPartsSearch.Web.Controllers
{
    [Authorize(Roles = Role.Seller)]
    public class SellerSearchQueryController : Controller
    {
        private readonly ISellerSearchQueryRepository _repository;
        private readonly ISellerGetter _sellerGetter;

        public SellerSearchQueryController(ISellerSearchQueryRepository repository, ISellerGetter sellerGetter)
        {
            this._repository = repository;
            this._sellerGetter = sellerGetter;
        }

        public ActionResult List(IPrincipal user, int page = 1, int pageSize = 20, bool? processed = null)
        {
            var seller = this._sellerGetter.GetSellerByUser(user);
            int totalCount;
            var queries = this._repository.GetBySeller(seller.Id, page, pageSize, out totalCount, processed);
            var result = new AutoPartsSearch.Web.ViewModels.SellerSearchQuery.ViewModel
                             {
                                 PagingInfo = new PagingInfo()
                                                  {
                                                      CurrentPage = page,
                                                      ItemsPerPage = pageSize,
                                                      TotalItems = totalCount
                                                  },
                                 SellerSearchQueries = queries
                             };
            ViewBag.Processed = processed == null ? "null" : processed.ToString().ToLower();
            ViewBag.IsSellerBlocked = (seller.IsBlocked || seller.IsExpired).ToString().ToLower();
            return View(result);
        }

        public PartialViewResult SellerWidjet(IPrincipal user)
        {
            return PartialView(GetWidgetModel(user));
        }

        [HttpPost]
        public RedirectToRouteResult Delete(int id, IPrincipal user)
        {
            var seller = this._sellerGetter.GetSellerByUser(user);
            if (!(seller.IsBlocked && seller.IsExpired))
            {
                this._repository.Delete(id, this._sellerGetter.GetCurrentSellerId(user));
            }
            return RedirectToAction("List");
        }

        public ViewResult Details(int id, IPrincipal user)
        {
            var seller = this._sellerGetter.GetSellerByUser(user);
            var result = this._repository.GetBySearchQueryAndSeller(id, seller.Id);
            ViewBag.IsSellerBlocked = (seller.IsBlocked || seller.IsExpired).ToString().ToLower();
            return View(result);
        }

       
        public JsonResult HasNewRequests(IPrincipal user)
        {
            var hasNewRequests = _repository.HasNewRequests(user.Identity.Name);
            return Json(hasNewRequests, JsonRequestBehavior.AllowGet);
        }

        public JsonResult UnreadCount(IPrincipal user)
        {
            var result = GetWidgetModel(user);
            return Json(result.UnprocessedCount, JsonRequestBehavior.AllowGet);
        }

        public JsonResult GetTheNewestItem(IPrincipal user)
        {
            var seller = this._sellerGetter.GetSellerByUser(user);
            int totalCount;
            var result = this._repository.GetBySeller(seller.Id, 1, 1, out totalCount, false).FirstOrDefault();
            return Json(result ?? new SellerSearchQuery(), JsonRequestBehavior.AllowGet);
        }
        
        private WidgetModel GetWidgetModel(IPrincipal user)
        {
            var seller = this._sellerGetter.GetSellerByUser(user);
            var unreadCount = this._repository.GetTotalCount(seller.Id, false);
            return new WidgetModel
            {
                SellerId = seller.Id,
                UnprocessedCount = unreadCount,
                IsBlocked = seller.IsBlocked,
                IsExpired = seller.IsExpired
            };
        }

    }
}
